<?php
/**
 * Description of UserFacade
 *
 * @author TheFaSTeR
 */
class UserFacade
{
    /**
     *
     * @param integer $id
     * @return User
     */
    public static function getUserById($id)
    {
        $user = User::model()->findByPk($id);
        if (is_null($user))
        {
            throw new CException("UserId not valid.");
        }
        return $user;
    }

    /**
     *
     * @param string $email
     * @return User
     */
    public static function getUserByEmail($email)
    {        
        $user = User::model()->find('email=:email', array(':email' => $email));
        return $user;
    }

    /**
     *
     * @param User $user
     */
    public static function AddUser(User $user)
    {
        if (!$user->user_id && !empty($user->password))
        {
            $user->password = $user->hashPassword($user->password);
        }
        $success = $user->save();

        $friendListModel = new FriendList();
        $friendListModel->user_id = $user->user_id;
        $friendListModel->save();
        return $success;
    }

    public static function isValidPassword(User $user, $password)
    {
        return $user->validatePassword($password);
    }
    
    public static function saveUser(User $user)
    {
        return $user->save();
    }
    
    public static function getUserAvatarUrl(User $model)
    {
        if ($model->avatar)
        {
            return '/uploads/' . $model->user_id . '/avatar/' . $model->avatar;
        }
        else
        {
            return '/images/no-photo.gif';
        }
    }
    
    public static function getUserAvatarPath(User $model = null)
    {
        return Yii::getPathOfAlias('webroot') . '/uploads/user/' . $model->user_id . '/avatar/';
    }
    
    public static function getUserUploadsDir(User $model)
    {
        return Yii::getPathOfAlias('webroot') . '/uploads/user/' . $model->user_id;
    }

    public static function saveUserAvatar($uploadImage)
    {
        $userId = Yii::app()->controller->getUserIdFromSession();
        $user = UserFacade::getUserById($userId);
        $oldImageId = $user->avatar_id;
        $user->avatar_id = $uploadImage->image_id;
        UserFacade::saveUser($user);
        if ($oldImageId != null)
        {
            UploadImageFacade::unsetImage($oldImageId);
        }
    }

    public static function deleteUserAvatar($user)
    {
        $imageId = $user->avatar_id;
        $user->avatar_id = null;
        $user->save();
        UploadImageFacade::unsetImage($imageId);
    }

    public static function getRandUsers($count)
    {
        $criteria = new CDbCriteria();
        $criteria->order = "rand()";
        $criteria->limit = $count;
        $user = User::model()->findAll($criteria);
        return $user;
    }

    public static function getFullNameUser($userId)
    {
        $user = UserFacade::getUserById($userId);
        return $user->name . " " . $user->surname;
    }

    public static function getDataProviderForSearch($name, $surname, $city)
    {
        $criteria = new CDbCriteria();
        if (!empty($name ))
            $criteria->addSearchCondition( 'name', $name, true, 'OR' );
        if (!empty($surname ))
            $criteria->addSearchCondition( 'surname', $surname, true, 'OR' );
        if (!empty($city ))
            $criteria->addSearchCondition( 'city', $city, true, 'OR' );

        $dataProvider = new CActiveDataProvider( 'User', array( 'criteria' => $criteria, ) );
        return $dataProvider;

    }

    public static function userIsVet($user)
    {
        return $user->is_vet;
    }
}
